<?php

class ProductController extends Zend_Controller_Action {

	protected $_product;
	protected $_mapper;
	public function init()
	{
		$id = (int) $this->_getParam('id', null);
		$this->_mapper = new Model_Product_Mapper();
		$this->_product =  $this->_mapper->load($id);
	}

	public function indexAction() {
	}

	public function addAction()
	{

	}

	public function parseAction()
	{
    	Zend_Layout::getMvcInstance()->disableLayout();
		$productXML = new Model_Product_XML();
		$start = (int) $this->_getParam('start',0);
		$end = (int) $this->_getParam('end',0);
		echo  $productXML->XmlToMysql($start, $end);
	}

	public function editAction()
	{
		$form = new Form_Product($this->_product);
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			if($form->isValid($this->getRequest()->getPost())){
				$this->_mapper->fill($this->_product,$this->getRequest()->getPost());
				$this->_mapper->save($this->_product);
				$this->_redirect('/product/view/id/' . $this->_product->id);
			}
		}
	}

	public function deleteAction()
	{
		$this->_product->delete();
		$this->_redirect($_SERVER['HTTP_REFERER']);
	}
	
	public function viewAction()
	{  
	    $category = $this->_mapper->getCategory($this->_product);
	    $this->view->headTitle()->append($category->name);
	    
	    $this->view->headTitle()->append($this->_product->brand. ', размер ' . $this->_product->size);
	    $this->view->assign(array(
	       'product' => $this->_product, 
	       'category'=>$category, 
	       'similar'=>$this->_mapper->getSimilar($this->_product)
	    ));
	}
}